// information/addPromotion/index.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    pmid: 0,
    title: '',
    startTime: '',
    endTime: '',
    discount: '',
    number: '',
    startDate: '',
    endDate: '',
    weeks: [{ value: 1, name: '星期一', checked: false }, { value: 2, name: '星期二', checked: false }, { value: 3, name: '星期三', checked: false }, { value: 4, name: '星期四', checked: false }, { value: 5, name: '星期五', checked: false }, { value: 6, name: '星期六', checked: false }, { value: 0, name: '星期天', checked: false }],
    remark: ''
  },

  submitForm: function () {
    let that = this;
    let title = that.data.title;
    if(title == ''){
      that.checkWrite('请输入促销标题');
      return;
    }
    let discount = that.data.discount;
    if(discount == '' || discount > 100 || discount <= 0){
      that.checkWrite('请输入大于0~100的促销折扣');
      return;
    }
    let stock = that.data.number;
    if(stock == '' || stock <= 0){
      that.checkWrite('请输入大于0的促销份数');
      return;
    }
    let selweek = '';
    let weeks = that.data.weeks;
    for(let i = 0; i < weeks.length; i++){
      if(weeks[i].checked){
        selweek += weeks[i].value + ',';
      }
    }
    if(selweek.length > 0){
      selweek = selweek.substr(0,selweek.length-1);
    }
    let contractDiscount = 100 - discount;
    if(contractDiscount.length>5){
      contractDiscount = parseFloat(contractDiscount).toFixed(2);
    }
    wx.request({
      url: app.globalData.host + '/api/Promotion/AddUpdateDiscount',
      data: {
        PmId: that.data.pmid,
        StoreId: app.globalData.storeId,
        Uid: app.globalData.userInfo.Uid,
        Name: title,
        StartTime: that.data.startDate,
        EndTime: that.data.endDate,
        StartHour: that.data.startTime,
        EndHour: that.data.endTime,
        Week: selweek,
        Stock: stock,
        ContractDiscount: contractDiscount,
        State: 0,
        CardItemId: 0,
        Remark: that.data.remark,
      },
      method: 'post',
      success: function (res) {
        if(res.data.Code == 0){
          wx.showToast({
            title: '保存成功',
            mask: true
          })
          wx.navigateTo({
            url: '../promotionList/index',
          })
        }else{
          wx.showModal({
            title: '提示',
            content: res.data.Message,
            showCancel: false
          })
        }
      },
    })
  },
  checkWrite: function(str){
    wx.showModal({
      title: '提示',
      content: str,
      showCancel: false
    })
  },
  // 获取当前年月
  CurentData: function () {
    var that = this;
    var now = new Date();
    var year = now.getFullYear(); //年
    var month = now.getMonth() + 1; //月
    var day = now.getDate(); //日
    var clock = year + "-";
    if (month < 10)
      clock += "0";
    clock += month + "-";
    if (day < 10)
      clock += "0";
    clock += day;
    that.setData({
      nowData: clock,
      startDate: clock,
      endDate: clock,
    })
  },
  // 获取当前时间分
  CurentTime: function () {
    var that = this;
    var now = new Date();
    var hh = now.getHours(); //时
    var mm = now.getMinutes(); //分
    var clock = "";
    if (hh < 10)
      clock += "0";
    clock += hh + ":";
    if (mm < 10) clock += '0';
    clock += mm;
    that.setData({
      nowtime: clock,
      startTime: clock + ':00',
      endTime: clock + ':59'
    })
  },

  bindTitle: function (e) {
    this.setData({
      title: e.detail.value
    })
  },
  bindDiscount: function (e) {
    this.setData({
      discount: e.detail.value
    })
  },
  bindNumber: function (e) {
    this.setData({
      number: e.detail.value
    })
  },
  bindStartTimeChange: function (e) {
    this.setData({
      startTime: e.detail.value + ':00'
    })
  },
  bindEndTimeChange: function (e) {
    this.setData({
      endTime: e.detail.value + ':59'
    })
  },

  bindStartDateChange: function (e) {
    this.setData({
      startDate: e.detail.value
    })
  },
  bindEndDateChange: function (e) {
    this.setData({
      endDate: e.detail.value
    })
  },
  selWeek: function (e) {
    let week = e.currentTarget.dataset.week;
    let weeks = this.data.weeks;
    for (let i = 0; i < weeks.length; i++) {
      if (week == weeks[i].value) {
        weeks[i].checked = !weeks[i].checked
      }
    }
    this.setData({
      weeks: weeks
    })
  },
  bindRemark: function (e) {
    this.setData({
      remark: e.detail.value
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.pmid) {
      this.data.pmid = options.pmid;
      this.getPromotionInfo(options.pmid);
    } else {
      this.CurentData();
      this.CurentTime();
    }
  },
  getPromotionInfo: function (pmid) {
    let that = this;
    let weeks = that.data.weeks;
    wx.request({
      url: app.globalData.host + '/api/Promotion/GetDiscount?pmId=' + pmid + '&uid=' + app.globalData.userInfo.Uid,
      success: function (res) {
        if (res.data.Code == 0) {
          let startDate = res.data.Data.StartTime.substr(0, 10);
          let endDate = res.data.Data.EndTime.substr(0, 10);
          let week = res.data.Data.Week;
          if (week) {
            let weekList = week.split(',');
            for (let i = 0; i < weekList.length; i++) {
              for (let j = 0; j < weeks.length; j++) {
                if (weekList[i] == weeks[j].value) {
                  weeks[j].checked = true;
                }
              }
            }
          }
          let discount = 100 - res.data.Data.ContractDiscount;
          if(discount.length > 5){
            discount = parseFloat(discount).toFixed(2);
          }
          that.setData({
            pmid: pmid,
            title: res.data.Data.Name,
            startTime: res.data.Data.StartHour,
            endTime: res.data.Data.EndHour,
            discount: discount,
            number: res.data.Data.Stock,
            startDate: startDate,
            endDate: endDate,
            weeks: weeks,
            remark: res.data.Data.Remark
          })
        }
      }
    })
  },


  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },


  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})